<?php

/**
 *  购物车
*/

namespace app\api\controller;
use app\api\service\CCNames as CCNamesService;
use app\api\service\ProductMerge as ProductMergeService;
use app\facade\Basic;
use think\Request;
use app\api\service\Carts as ServiceCarts;

class Carts extends BaseController
{

    /**
     *  购物车列表
     */
    public function list(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['page'] = $request->param('page',1);
        $param['listRows'] = $request->param('listRows',15);
        $param['lang'] = $request->header('think-lang');
        /* 验证 */
        $this->validateData($param,'Carts.list');

        /* 登录操作 */
        $ServiceCarts = new ServiceCarts();
        $result = $ServiceCarts->list($param);
        return Basic::json($result);

    }


    /**
     *  加入购物车
    */
    public function wholesalerAdd(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['product_id'] = $request->param('product_id',null);
        $param['warehouse_id'] = $request->param('warehouse_id',null);
        $param['is_futures'] = $request->param('is_futures',null);
        $param['arrival_date'] = $request->param('arrival_date',null);
        $param['quantity'] = $request->param('quantity',1);

        /* 验证 */
        $this->validateData($param,'Carts.add');

        /* 登录操作 */
        $ServiceCarts = new ServiceCarts();
        $result = $ServiceCarts->wadd($param);
        return Basic::json($result);
    }


    /**
     *  加入购物车
    */
    public function add(Request $request)
    {

        $param['user_id'] = $request->user_id;
        $param['id'] = $request->param('id',null);
        $param['sku_id'] = $request->param('sku_id',null);
        $param['batch'] = $request->param('batch',null);
        $param['quantity'] = $request->param('quantity',1);

        // 如果需要同时获取多个IP信息
        $param['ip'] = getRealIp($request);

        /* 测试数据 */
 //       $param['ip'] = '182.239.127.137';

        /* 验证 */
        //$this->validateData($param,'Carts.add');

        /* 登录操作 */
        $ServiceCarts = new ServiceCarts();
        $result = $ServiceCarts->add($param);
        return Basic::json($result);
    }


    /**
     *  bulk order 加入购物车
     */
    public function bulkAdd(Request $request)
    {

        $param['user_id'] = $request->user_id;
        $param['product_id'] = $request->param('product_id',null);
        $param['warehouse_id'] = $request->param('warehouse_id',null);
        $param['is_futures'] = $request->param('is_futures',null);
        $param['quantity'] = $request->param('quantity',1);
        $param['arrival_date'] = $request->param('arrival_date',null);

        /* 验证 */
        $this->validateData($param,'Carts.add');

        /* 登录操作 */
        $ServiceCarts = new ServiceCarts();
        $result = $ServiceCarts->bulkAdd($param);
        return Basic::json($result);
    }

    /**
     *  更新购物车
    */
    public function update(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['id'] = $request->param('id',null);
        $param['sku'] = $request->param('sku',null);
        $param['quantity'] = $request->param('quantity',null);
        $param['warehouse_id'] = $request->param('warehouse_id',null);

        /* 验证 */
        $this->validateData($param,'Carts.update');

        /* 登录操作 */
        $ServiceCarts = new ServiceCarts();
        $result = $ServiceCarts->update($param);
        return Basic::json($result);

    }

    /**
     *  移除购物车
    */
    public function remove(Request $request)
    {
        $param['user_id'] = $request->user_id;
        $param['id'] = $request->param('id',null);

        /* 验证 */
        $this->validateData($param,'Carts.remove');

        /* 登录操作 */
        $ServiceLogin = new ServiceCarts();
        $result = $ServiceLogin->remove($param);
        return Basic::json($result);
    }

    /**
     *  清空购物车
    */
    public function clearAll(Request $request)
    {
        $param['user_id'] = $request->user_id;

        /* 验证 */
        $this->validateData($param,'Carts.clearAll');

        /* 登录操作 */
        $ServiceLogin = new ServiceCarts();
        $result = $ServiceLogin->clearAll($param);
        return Basic::json($result);
    }


}
